package com.dfqy.controller;

import com.dfqy.pojo.SymType;
import com.dfqy.service.SymTypeService;
import com.dfqy.vo.Page;
import com.dfqy.vo.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/symtype")
@Api("症状类型管理")
public class SymTypeController {

    @Autowired
    private SymTypeService symTypeService;

    @RequestMapping(value = "allsymtype")
    public Page<SymType> paging(@RequestParam(defaultValue = "1") int page, int limit, String symType,String poster){
        Page<SymType> symTypePage = symTypeService.paging(page,limit,symType,poster);
        return symTypePage;
    }

    @PostMapping(value = "/addsymtype",produces = "application/json;charset=UTF-8")
    public Result add(@RequestBody SymType symType){

        int r = symTypeService.add(symType);
        if (r > 0)
            return Result.success();
        return Result.error("新增失败");
    }

    @RequestMapping(value = "/findsymtype/{id}",produces = "application/json;charset=UTF-8")
    public Result findSymType(@PathVariable long id){
        System.out.println("要修改的id是："+id);
        SymType symType = symTypeService.getInfoById(id);
        return symType == null ? Result.error("该管理员不存在") : Result.success(symType);
    }

    @RequestMapping(value = "/updatesymtype",produces = "application/json;charset=UTF-8")
    public Result update(@RequestBody SymType symType){

        int r = symTypeService.update(symType);
        if (r > 0)
            return Result.success();
        return Result.error("修改失败");
    }

    @RequestMapping(value = "/delsymtype",produces = "application/json;charset=UTF-8")
    public Result del(String id){
        String[] idStr = id.split(",");
        long[] ids = new long[idStr.length];
        for (int i = 0; i < idStr.length; i++) {
            ids[i] = Long.parseLong(idStr[i]);
        }
        int r = symTypeService.delete(ids);
        return r > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    //修改启用状态通过滑动按钮
    @RequestMapping(value = "/changeIsValid",produces = "application/json;charset=UTF-8")
    public Result changeIsValid(long id,byte isvalid){
        isvalid = (byte) (isvalid>0?1:0);
        SymType byId = symTypeService.getInfoById(id);
        byId.setIsValid(isvalid);
        int r = symTypeService.update(byId);
        if (r > 0)
            return Result.success();
        return Result.error("上/下架失败");
    }

    //增加时前端症状类型验证
    @RequestMapping(value = "/addchecksymtype",produces = "application/json;charset=UTF-8")
    public Result addCheckTitle(String symType){
        boolean r = symTypeService.addchecktitle(symType);
        return r ? Result.error("症状类型已经存在") : Result.success();
    }

    //修改时前端症状类型验证
    @RequestMapping(value = "/checksymtype",produces = "application/json;charset=UTF-8")
    public Result checkTitle(long id,String symType){
        boolean r = symTypeService.checktitle(symType,id);
        return r ? Result.error("症状类型已经存在") : Result.success();
    }
}
